home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / INTERNET / WI9609FT.ZIP / CONVERT.JAV next >
Encoding:
Text File  |  1996-07-14  |  1.5 KB  |  56 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.lang.*;
  4.  
  5. public class convert extends Applet {
  6.  
  7.    TextField entry;
  8.    TextArea result;
  9.    final static String CONVERT = "Convert";
  10.  
  11.    public void init() {
  12.       entry = new TextField(10) ;
  13.       add(entry);
  14.       add(new Button(CONVERT) );
  15.       result = new TextArea() ;
  16.       add(result);
  17.    }
  18.  
  19.    public boolean action(Event evt, Object arg) {
  20.       int val;
  21.  
  22.       if (arg.equals(CONVERT)) {
  23.          String num = entry.getText();
  24.  
  25.    /*First, we try to conver the text to a 
  26.     value in base 10.*/
  27.       try {
  28.          val = Integer.parseInt(num); 
  29.          result.setText("In base 10, twice your number ");
  30.          result.appendText("is: "+ 2*val+".\n");
  31.        }
  32.       catch(Exception e) {
  33.          result.setText("Your number is not valid in ");
  34.          result.appendText("base 10.\n");
  35.       }
  36.  
  37.       /* Next, we try to convert the text to 
  38.        a binary number (base 2). */
  39.       try {
  40.          val = Integer.parseInt(num,2);
  41.          result.appendText("In binary, twice your number ");
  42.          result.appendText("is: "+ 2*val + ".\n");
  43.       }
  44.       catch (Exception e) {
  45.          result.appendText("Your number is not a valid "); 
  46.          result.appendText("binary number.\n");
  47.       }
  48.       return true;  // returns true only if the "Convert"
  49.                     // button was pressed (meaning that we
  50.                     // dealt with the event
  51.       }
  52.        return false;   // returns false otherwise
  53.    }
  54. }
  55.  
  56.